﻿/****************************************************************************
 * Application   : Freekick Desktop Manager
 * Author        : sfreekick
 * Email         : sfreekick@gmail.com
 * Copyright     : (C) 2008 sfreekick
 * **************************************************************************/

/****************************************************************************
 *  This file is part of FreeKickManager.
 *
 *  FreeKickManager is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  FreeKickManager is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with FreeKickManager. If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace FreeKickManager
{
    public partial class FrmGestionVistas : Form
    {
        private ListViewSettings lvs = null;
        private ListView lvJugadores = null;
        private ListView lvPorteros = null;

        public FrmGestionVistas(ListViewSettings lvs, ListView lvJugadores, ListView lvPorteros)
        {
            InitializeComponent();

            this.lvs = lvs;
            this.lvJugadores = lvJugadores;
            this.lvPorteros = lvPorteros;

            if (lvs != null)
                txtNueva.Text = lvs.nombreVista;

            lstExistentes.Items.Clear();
            DirectoryInfo di = new DirectoryInfo("views\\");
            foreach (FileInfo fi in di.GetFiles())
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);

                ListViewSettings lvs2 = (ListViewSettings)formatter.Deserialize(stream);
                stream.Close();

                lstExistentes.Items.Add(lvs2);
            }
        }

        private void btnAnadir_Click(object sender, EventArgs e)
        {
            if (!txtNueva.Text.Equals(""))
            {
                ListViewSettings lvsnueva = new ListViewSettings(txtNueva.Text.Trim(), lvJugadores, lvPorteros);

                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("views\\" + txtNueva.Text.Trim() + ".fkv", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, lvsnueva);
                stream.Close();

                lstExistentes.Items.Add(txtNueva.Text);
            }
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
            ListViewSettings lvsnueva = new ListViewSettings(txtNueva.Text.Trim(), lvJugadores, lvPorteros);

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("views\\" + lvs.nombreVista.Trim() + ".fkv", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, lvsnueva);
            stream.Close();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (lstExistentes.SelectedIndex != -1)
            {
                string fichero = ((ListViewSettings)lstExistentes.SelectedItem).nombreVista;
                lstExistentes.Items.RemoveAt(lstExistentes.SelectedIndex);
                File.Delete("views\\" + fichero + "fkv");
            }
        }
    }
}
